package com.joysuch.wwyt.risk.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.joysuch.wwyt.bp.entity.BpDepart;
import com.joysuch.wwyt.bp.entity.investigate.BpInvestigatePlan;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.util.SimpleDateFormatCache;
import com.joysuch.wwyt.core.entity.BaseTypeGroupItem;
import com.joysuch.wwyt.core.entity.BaseUser;
import com.joysuch.wwyt.core.repository.BaseUserDao;
import com.joysuch.wwyt.risk.bean.*;
import com.joysuch.wwyt.risk.entity.*;
import com.joysuch.wwyt.risk.repository.*;
import com.joysuch.wwyt.risk.service.*;
import com.joysuch.wwyt.risk.util.RiskEvalExcelUtil;
import com.joysuch.wwyt.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * @Author ZhaoQiuWei
 * @Date 2022/2/16 16:59
 */
@Service
@Slf4j
public class RiskPointEvalExcelExportServiceImpl implements RiskPointEvalExcelExportService {

    @Autowired
    private RiskEvaluationAnalyzeWayDao analyzeWayDao;
    @Autowired
    private RiskEvaluationAnalyzeMethodDao analyzeMethodDao;
    @Autowired
    private RiskEvaluationTargetTypeDao targetTypeDao;
    @Autowired
    private RiskEvaluationTargetItemDao targetItemDao;
    @Autowired
    private RiskPointTypeDao riskPointTypeDao;
    @Autowired
    private RiskEvaluationBusinessService riskEvaluationBusinessService;
    @Autowired
    private RiskPointEvaluationBusinessDao pointEvaluationBusinessDao;
    @Autowired
    private RiskPointEvaluationDetailDao riskPointEvaluationDetailDao;
    @Autowired
    private RiskControlLevelService riskControllevelService;
    @Autowired
    private RiskMeasureTypeService riskMeasureTypeService;
    @Autowired
    private RiskStorePointEvalExcelExportService riskStorePointEvalExcelExportService;


    @Override
    public RiskExcelModelBean createSheetData(RiskExcelModelBean bean, Long analyzeWayId, Integer isTemplate, Integer shared, ExcelExportToolMapBean toolBean, Integer measureTypeFlag, Integer storePointFlag) {
        SXSSFWorkbook workbook = bean.getWorkbook();
        Integer sheetIndex = bean.getSheetIndex() == null ? Integer.valueOf(0) : bean.getSheetIndex();
        //设置sheet 某一列为文本格式
        CellStyle cellStyle = workbook.createCellStyle();
        DataFormat format = workbook.createDataFormat();
        cellStyle.setDataFormat(format.getFormat("@"));
        //      if (isTemplate != null && isTemplate.intValue() == 1) {
        //风险点数据导出
        if (measureTypeFlag == 0) {
            workbook = getShanDongNewPointData(workbook, bean, analyzeWayId, toolBean, isTemplate, sheetIndex);
        } else {
            workbook = getNewPointData(workbook, bean, analyzeWayId, toolBean, isTemplate, sheetIndex);
        }
        // } else if (isTemplate != null && isTemplate.intValue() == 2) {
        // workbook = riskStorePointEvalExcelExportService.getOldItemData(workbook, sheet, analyze, analyzeBeanList, shared);
        //  }
        bean.setWorkbook(workbook);

        return bean;

    }

    private SXSSFWorkbook getShanDongNewPointData(SXSSFWorkbook workbook, RiskExcelModelBean bean, Long analyzeWayId, ExcelExportToolMapBean toolBean, Integer isTemplate, Integer sheetIndex) {

        //设置sheet 某一列为文本格式
        CellStyle cellStyle = workbook.createCellStyle();
        DataFormat format = workbook.createDataFormat();
        cellStyle.setDataFormat(format.getFormat("@"));

        //分析方法
        RiskEvaluationAnalyzeWay analyze = analyzeWayDao.findById(analyzeWayId).orElseThrow(() -> new IllegalArgumentException("分析方法不存在"));
        List<RiskEvaluationAnalyzeMethod> analyzeWayList = analyzeMethodDao.findByWayId(analyzeWayId);
        if (analyze.getEvaluationId() == null) {
            throw new IllegalArgumentException("该分析方法未绑定评估方法");
        }

        //评估方法
        RiskEvaluationAnalyzeWay evaluation = analyzeWayDao.findById(analyze.getEvaluationId()).orElseThrow(() -> new IllegalArgumentException("评估方法不存在"));
        List<RiskEvaluationAnalyzeMethod> evaluationWayList = analyzeMethodDao.findByWayId(evaluation.getId());

        //管控措施
        if (bean.getMaxTypeLevel() == null || bean.getMaxTypeLevel() == 0) {
            throw new IllegalArgumentException("管控措施尚未配置");
        }

        boolean stablePointContrFlag = toolBean.getStablePointContrFlag();
        //评估数据导出小标题 为以后数据导出做准备
        List<RiskExportDataBean> analyzeBeanList = new ArrayList<>();
        SXSSFSheet sheet = null;
        try {
            sheet = workbook.createSheet(analyze.getName());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new IllegalArgumentException("不支持导出分析方法[" + analyze.getName() + "]");
        }
        SXSSFRow row = null;
        SXSSFCell cell = null;

        //标题
        row = sheet.createRow(0);
        row.setHeightInPoints(20);
        //区域index
        int districtNameIndex = 0;
        //风险点index
        int pointCodeIndex = 0;
        int pointNameIndex = 1;
        int pointTypeIndex = 2;
        //序号
        int index = 3;
        //方法
        int stableIndex = 0;
        int controlIndex = index + analyzeWayList.size();
        //管控措施
        int measureTypeIndex = controlIndex + evaluationWayList.size();
        int evalTimeIndex = measureTypeIndex + 5;
        //评估人index
        int evalUserIndex = evalTimeIndex - 1;
        //责任单位index
        int resDepartIndex = 0;
        //管控级别
        int controLevelIndex = 0;
        List<RiskExcelTitleBean> titles = new ArrayList<>();
        index = 4;

        pointCodeIndex = 0;
        pointNameIndex = 1;
        pointTypeIndex = 2;
        districtNameIndex = 3;
        measureTypeIndex = index + analyzeWayList.size();
        if (stablePointContrFlag) {
            stableIndex = index + analyzeWayList.size() + toolBean.getTopTypeList().size();
            controlIndex = stableIndex + evaluationWayList.size();
        } else {
            controlIndex = index + analyzeWayList.size() + toolBean.getTopTypeList().size();
        }
        evalTimeIndex = controlIndex + evaluationWayList.size() + 4;
        evalUserIndex = evalTimeIndex - 2;
        resDepartIndex = evalTimeIndex - 3;
        controLevelIndex = evalTimeIndex - 1;

        titles = excelTitleBean("风险分析对象名称", true, titles, districtNameIndex);
        analyzeBeanList.add(getExportDataBean(null, null, "风险分析对象名称", districtNameIndex));
        sheet.setDefaultColumnStyle(districtNameIndex, cellStyle);
        titles = excelTitleBean("风险单元编号", false, titles, pointCodeIndex);
        analyzeBeanList.add(getExportDataBean(null, null, "风险单元编号", pointCodeIndex));
        sheet.setDefaultColumnStyle(pointCodeIndex, cellStyle);
        titles = excelTitleBean("风险单元名称", true, titles, pointNameIndex);
        analyzeBeanList.add(getExportDataBean(null, null, "风险单元名称", pointNameIndex));
        sheet.setDefaultColumnStyle(pointNameIndex, cellStyle);
        titles = excelTitleBean("风险单元类型", true, titles, pointTypeIndex);
        analyzeBeanList.add(getExportDataBean(null, null, "风险单元类型", pointTypeIndex));
        List<RiskPointType> pointTypes = riskPointTypeDao.findByParentId(null);
        if (!CollectionUtils.isEmpty(pointTypes)) {
            List<String> collect = pointTypes.stream().map(e -> e.getName()).collect(Collectors.toList());
            RiskEvalExcelUtil.getSelectData(workbook, collect, sheet, 1, 100, pointTypeIndex, sheetIndex);
            sheetIndex = sheetIndex + 1;
        }
        sheet.setDefaultColumnStyle(pointTypeIndex, cellStyle);
        titles = excelTitleBean("序号", true, titles, index);
        analyzeBeanList.add(getExportDataBean(null, null, "序号", index));
        sheet.setDefaultColumnStyle(index, cellStyle);

        //分析方法标题
        if (!CollectionUtils.isEmpty(analyzeWayList)) {
            analyzeWayList = analyzeWayList.stream().sorted(Comparator.comparing(RiskEvaluationAnalyzeMethod::getOrderNumber, Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList());
        }
        for (int i = 0; i < analyzeWayList.size(); i++) {
            RiskEvaluationAnalyzeMethod method = analyzeWayList.get(i);
            if (method.getIsRequired() != null && method.getIsRequired() == 1) {
                titles = excelTitleBean(method.getName(), true, titles, index + i + 1);
            } else {
                titles = excelTitleBean(method.getName(), false, titles, index + i + 1);
            }
            List<String> list = new ArrayList<>();
            if (method.getContentType() != null && method.getContentType() == 1) {
                List<RiskEvaluationTargetType> targetTypeList = targetTypeDao.findByMethodId(method.getId());
                if (!CollectionUtils.isEmpty(targetTypeList)) {
                    for (RiskEvaluationTargetType targetType : targetTypeList) {
                        List<RiskEvaluationTargetItem> targetItemList = targetItemDao.findByTypeId(targetType.getId());
                        for (RiskEvaluationTargetItem item : targetItemList) {
                            list.add(item.getName());
                        }
                    }
                }
                if (!CollectionUtils.isEmpty(list)) {
                    RiskEvalExcelUtil.getSelectData(workbook, list, sheet, 1, 100, index + i + 1, sheetIndex);
                    sheetIndex = sheetIndex + 1;
                }
            }
            analyzeBeanList.add(getExportDataBean(method.getId(), method.getName(), "分析方法", index + i + 1));
        }

        if (!CollectionUtils.isEmpty(evaluationWayList)) {
            evaluationWayList = evaluationWayList.stream()
                    .sorted(Comparator.comparing(RiskEvaluationAnalyzeMethod::getOrderNumber, Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList());
        }

        if (stablePointContrFlag) {
            for (int i = 0; i < evaluationWayList.size(); i++) {
                RiskEvaluationAnalyzeMethod method = evaluationWayList.get(i);
                if (method.getIsRequired() != null && method.getIsRequired() == 1) {
                    titles = excelTitleBean("[固有评估]" + method.getCode(), true, titles, stableIndex + i + 1);
                } else {
                    titles = excelTitleBean("[固有评估]" + method.getCode(), false, titles, stableIndex + i + 1);
                }
                List<String> list = new ArrayList<>();
                if (method.getContentType() != null && method.getContentType() == 0) {
                    List<RiskEvaluationTargetType> targetTypeList = targetTypeDao.findByMethodId(method.getId());
                    if (!CollectionUtils.isEmpty(targetTypeList)) {
                        for (RiskEvaluationTargetType targetType : targetTypeList) {
                            List<RiskEvaluationTargetItem> targetItemList = targetItemDao.findByTypeId(targetType.getId());
                            for (RiskEvaluationTargetItem item : targetItemList) {
                                if (!list.contains(item.getScore() + "")) {
                                    list.add(item.getScore() + "");
                                }
                            }
                        }
                    }
                    if (!CollectionUtils.isEmpty(list)) {
                        RiskEvalExcelUtil.getSelectData(workbook, list, sheet, 1, 100, stableIndex + i + 1, sheetIndex);
                        sheetIndex = sheetIndex + 1;
                    }
                }
                analyzeBeanList.add(getExportDataBean(method.getId(), method.getName(), "固有评估方法", stableIndex + i + 1));
            }
        }

        for (int i = 0; i < evaluationWayList.size(); i++) {
            RiskEvaluationAnalyzeMethod method = evaluationWayList.get(i);
            if (method.getIsRequired() != null && method.getIsRequired() == 1) {
                titles = excelTitleBean(method.getCode(), true, titles, controlIndex + i + 1);
            } else {
                titles = excelTitleBean(method.getCode(), false, titles, controlIndex + i + 1);
            }
            List<String> list = new ArrayList<>();
            if (method.getContentType() != null && method.getContentType() == 0) {
                List<RiskEvaluationTargetType> targetTypeList = targetTypeDao.findByMethodId(method.getId());
                if (!CollectionUtils.isEmpty(targetTypeList)) {
                    for (RiskEvaluationTargetType targetType : targetTypeList) {
                        List<RiskEvaluationTargetItem> targetItemList = targetItemDao.findByTypeId(targetType.getId());
                        for (RiskEvaluationTargetItem item : targetItemList) {
                            if (!list.contains(item.getScore() + "")) {
                                list.add(item.getScore() + "");
                            }
                        }
                    }
                }
                if (!CollectionUtils.isEmpty(list)) {
                    RiskEvalExcelUtil.getSelectData(workbook, list, sheet, 1, 100, controlIndex + i + 1, sheetIndex);
                    sheetIndex = sheetIndex + 1;
                }
            }
            analyzeBeanList.add(getExportDataBean(method.getId(), method.getName(), "控制评估方法", controlIndex + i + 1));
        }

        List<RiskMeasureType> topTypeList = toolBean.getTopTypeList();
        if (CollectionUtils.isEmpty(topTypeList)) {
            throw new IllegalArgumentException("请至少配置一条管控措施类型");
        }
        //管控措施
        for (int i = 0; i < topTypeList.size(); i++) {
            RiskMeasureType type = topTypeList.get(i);
            titles = excelTitleBean(type.getMeasureTypeName(), false, titles, measureTypeIndex + i + 1);
            analyzeBeanList.add(getExportDataBean(null, type.getMeasureTypeName(), "管控措施类型", measureTypeIndex + i + 1));
            sheet.setDefaultColumnStyle(measureTypeIndex + i + 1, cellStyle);
        }

        titles = excelTitleBean("评估人", false, titles, evalUserIndex);
        analyzeBeanList.add(getExportDataBean(null, "评估人", "评估人", evalUserIndex));
        sheet.setDefaultColumnStyle(evalUserIndex, cellStyle);


        titles = excelTitleBean("评估日期", true, titles, evalTimeIndex);
        analyzeBeanList.add(getExportDataBean(null, "评估日期", "评估日期", evalTimeIndex));
        sheet.setDefaultColumnStyle(evalTimeIndex, cellStyle);

        titles = excelTitleBean("责任单位", false, titles, resDepartIndex);
        analyzeBeanList.add(getExportDataBean(null, "责任单位", "责任单位", resDepartIndex));
        sheet.setDefaultColumnStyle(resDepartIndex, cellStyle);

        titles = excelTitleBean("管控级别", false, titles, controLevelIndex);
        analyzeBeanList.add(getExportDataBean(null, "管控级别", "管控级别", controLevelIndex));
        sheet.setDefaultColumnStyle(controLevelIndex, cellStyle);
        sheetIndex = fillControllLevelList(controLevelIndex, workbook, sheet, sheetIndex);
        List<SimpleSelectListBean> controlLevelList = riskEvaluationBusinessService.getControlLevelList();
        List<String> s1 = controlLevelList.stream().map(e -> e.getLabel()).collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(s1)) {
            RiskEvalExcelUtil.getSelectData(workbook, s1, sheet, 1, 100, controLevelIndex, sheetIndex);
            sheetIndex = sheetIndex + 1;
        }
        //创建cell
        titles = titles.stream().sorted(Comparator.comparing(RiskExcelTitleBean::getIndex)).collect(Collectors.toList());
        for (int i = 0; i < titles.size(); i++) {
            cell = row.createCell(i);
            cell.setCellValue(titles.get(i).getTitle());
            if (titles.get(i).getIsRed()) {
                cell.setCellStyle(title(workbook, true));
            } else {
                cell.setCellStyle(title(workbook, false));
            }
        }
        //设置列宽 长度 = 风险点+风险点名称+分析方法+评估时间+评估方法+管控措施字段个数+分级管控字段个数
        for (int i = 0; i < titles.size(); i++) {
            sheet.setColumnWidth(i, 20 * 256);
        }
        if (isTemplate != null && isTemplate.intValue() == 1) {
            workbook = getShanDongNewPointItemData(workbook, sheet, analyzeBeanList, toolBean, analyzeWayId, stablePointContrFlag);
        }
        bean.setSheetIndex(sheetIndex);
        return workbook;
    }

    private SXSSFWorkbook getNewPointData(SXSSFWorkbook workbook, RiskExcelModelBean bean, Long analyzeWayId, ExcelExportToolMapBean toolBean, Integer isTemplate, Integer sheetIndex) {
        //设置sheet 某一列为文本格式
        CellStyle cellStyle = workbook.createCellStyle();
        DataFormat format = workbook.createDataFormat();
        cellStyle.setDataFormat(format.getFormat("@"));

        //分析方法
        RiskEvaluationAnalyzeWay analyze = analyzeWayDao.findById(analyzeWayId).orElseThrow(() -> new IllegalArgumentException("分析方法不存在"));
        List<RiskEvaluationAnalyzeMethod> analyzeWayList = analyzeMethodDao.findByWayId(analyzeWayId);
        if (analyze.getEvaluationId() == null) {
            throw new IllegalArgumentException("该分析方法未绑定评估方法");
        }

        //评估方法
        RiskEvaluationAnalyzeWay evaluation = analyzeWayDao.findById(analyze.getEvaluationId()).orElseThrow(() -> new IllegalArgumentException("评估方法不存在"));
        List<RiskEvaluationAnalyzeMethod> evaluationWayList = analyzeMethodDao.findByWayId(evaluation.getId());

        //管控措施
        if (bean.getMaxTypeLevel() == null || bean.getMaxTypeLevel() == 0) {
            throw new IllegalArgumentException("管控措施尚未配置");
        }

        boolean stablePointContrFlag = toolBean.getStablePointContrFlag();
        //评估数据导出小标题 为以后数据导出做准备
        List<RiskExportDataBean> analyzeBeanList = new ArrayList<>();
        SXSSFSheet sheet = null;
        try {
            sheet = workbook.createSheet(analyze.getName());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new IllegalArgumentException("不支持导出分析方法[" + analyze.getName() + "]");
        }
        SXSSFRow row = null;
        SXSSFCell cell = null;

        //标题
        row = sheet.createRow(0);
        row.setHeightInPoints(20);
        //区域index
        int districtNameIndex = 0;
        //风险点index
        int pointNameIndex = 3;
        int pointTypeIndex = 4;
        //序号
        int index = 5;
        //方法
        int stableIndex = 0;
        int controlIndex = index + analyzeWayList.size();
        //管控措施
        int measureTypeIndex = controlIndex + evaluationWayList.size();
        int evalTimeIndex = 0;
        //隐患排查内容
        int dangerContentIndex = 0;
        List<RiskExcelTitleBean> titles = new ArrayList<>();
        if (stablePointContrFlag) {
            stableIndex = index + analyzeWayList.size();
            controlIndex = stableIndex + evaluationWayList.size();
            measureTypeIndex = controlIndex + evaluationWayList.size();
        }

        titles = excelTitleBean("风险分析对象", true, titles, districtNameIndex);
        analyzeBeanList.add(getExportDataBean(null, null, "风险分析对象", districtNameIndex));
        sheet.setDefaultColumnStyle(districtNameIndex, cellStyle);
        titles = excelTitleBean("责任部门", false, titles, pointNameIndex - 2);
        analyzeBeanList.add(getExportDataBean(null, null, "区域责任部门", pointNameIndex - 2));
        sheet.setDefaultColumnStyle(pointNameIndex - 2, cellStyle);
        titles = excelTitleBean("责任人", false, titles, pointNameIndex - 1);
        analyzeBeanList.add(getExportDataBean(null, null, "区域责任人", pointNameIndex - 1));
        sheet.setDefaultColumnStyle(pointNameIndex - 1, cellStyle);
        titles = excelTitleBean("风险分析单元", true, titles, pointNameIndex);
        analyzeBeanList.add(getExportDataBean(null, null, "风险分析单元", pointNameIndex));
        sheet.setDefaultColumnStyle(pointNameIndex, cellStyle);
        titles = excelTitleBean("风险单元类型", true, titles, pointTypeIndex);
        analyzeBeanList.add(getExportDataBean(null, null, "风险单元类型", pointTypeIndex));
        List<RiskPointType> pointTypes = riskPointTypeDao.findByParentId(null);
        if (!CollectionUtils.isEmpty(pointTypes)) {
            List<String> collect = pointTypes.stream().map(e -> e.getName()).collect(Collectors.toList());
            RiskEvalExcelUtil.getSelectData(workbook, collect, sheet, 1, 100, pointTypeIndex, sheetIndex);
            sheetIndex = sheetIndex + 1;
        }
        sheet.setDefaultColumnStyle(pointTypeIndex, cellStyle);
        titles = excelTitleBean("序号", true, titles, index);
        analyzeBeanList.add(getExportDataBean(null, null, "序号", index));
        sheet.setDefaultColumnStyle(index, cellStyle);

        //分析方法标题
        if (!CollectionUtils.isEmpty(analyzeWayList)) {
            analyzeWayList = analyzeWayList.stream().sorted(Comparator.comparing(RiskEvaluationAnalyzeMethod::getOrderNumber, Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList());
        }
        for (int i = 0; i < analyzeWayList.size(); i++) {
            RiskEvaluationAnalyzeMethod method = analyzeWayList.get(i);
            if (method.getIsRequired() != null && method.getIsRequired() == 1) {
                titles = excelTitleBean(method.getName(), true, titles, index + i + 1);
            } else {
                titles = excelTitleBean(method.getName(), false, titles, index + i + 1);
            }
            List<String> list = new ArrayList<>();
            if (method.getContentType() != null && method.getContentType() == 1) {
                List<RiskEvaluationTargetType> targetTypeList = targetTypeDao.findByMethodId(method.getId());
                if (!CollectionUtils.isEmpty(targetTypeList)) {
                    for (RiskEvaluationTargetType targetType : targetTypeList) {
                        List<RiskEvaluationTargetItem> targetItemList = targetItemDao.findByTypeId(targetType.getId());
                        for (RiskEvaluationTargetItem item : targetItemList) {
                            list.add(item.getName());
                        }
                    }
                }
                if (!CollectionUtils.isEmpty(list)) {
                    RiskEvalExcelUtil.getSelectData(workbook, list, sheet, 1, 100, index + i + 1, sheetIndex);
                    sheetIndex = sheetIndex + 1;
                }
            }
            analyzeBeanList.add(getExportDataBean(method.getId(), method.getName(), "分析方法", index + i + 1));
        }

        if (!CollectionUtils.isEmpty(evaluationWayList)) {
            evaluationWayList = evaluationWayList.stream()
                    .sorted(Comparator.comparing(RiskEvaluationAnalyzeMethod::getOrderNumber, Comparator.nullsLast(Integer::compareTo))).collect(Collectors.toList());
        }

        if (stablePointContrFlag) {
            for (int i = 0; i < evaluationWayList.size(); i++) {
                RiskEvaluationAnalyzeMethod method = evaluationWayList.get(i);
                if (method.getIsRequired() != null && method.getIsRequired() == 1) {
                    titles = excelTitleBean("[固有评估]" + method.getCode(), true, titles, stableIndex + i + 1);
                } else {
                    titles = excelTitleBean("[固有评估]" + method.getCode(), false, titles, stableIndex + i + 1);
                }
                List<String> list = new ArrayList<>();
                if (method.getContentType() != null && method.getContentType() == 0) {
                    List<RiskEvaluationTargetType> targetTypeList = targetTypeDao.findByMethodId(method.getId());
                    if (!CollectionUtils.isEmpty(targetTypeList)) {
                        for (RiskEvaluationTargetType targetType : targetTypeList) {
                            List<RiskEvaluationTargetItem> targetItemList = targetItemDao.findByTypeId(targetType.getId());
                            for (RiskEvaluationTargetItem item : targetItemList) {
                                if (!list.contains(item.getScore() + "")) {
                                    list.add(item.getScore() + "");
                                }
                            }
                        }
                    }
                    if (!CollectionUtils.isEmpty(list)) {
                        RiskEvalExcelUtil.getSelectData(workbook, list, sheet, 1, 100, stableIndex + i + 1, sheetIndex);
                        sheetIndex = sheetIndex + 1;
                    }
                }
                analyzeBeanList.add(getExportDataBean(method.getId(), method.getName(), "固有评估方法", stableIndex + i + 1));
            }
        }

        for (int i = 0; i < evaluationWayList.size(); i++) {
            RiskEvaluationAnalyzeMethod method = evaluationWayList.get(i);
            if (method.getIsRequired() != null && method.getIsRequired() == 1) {
                titles = excelTitleBean(method.getCode(), true, titles, controlIndex + i + 1);
            } else {
                titles = excelTitleBean(method.getCode(), false, titles, controlIndex + i + 1);
            }
            List<String> list = new ArrayList<>();
            if (method.getContentType() != null && method.getContentType() == 0) {
                List<RiskEvaluationTargetType> targetTypeList = targetTypeDao.findByMethodId(method.getId());
                if (!CollectionUtils.isEmpty(targetTypeList)) {
                    for (RiskEvaluationTargetType targetType : targetTypeList) {
                        List<RiskEvaluationTargetItem> targetItemList = targetItemDao.findByTypeId(targetType.getId());
                        for (RiskEvaluationTargetItem item : targetItemList) {
                            if (!list.contains(item.getScore() + "")) {
                                list.add(item.getScore() + "");
                            }
                        }
                    }
                }
                if (!CollectionUtils.isEmpty(list)) {
                    RiskEvalExcelUtil.getSelectData(workbook, list, sheet, 1, 100, controlIndex + i + 1, sheetIndex);
                    sheetIndex = sheetIndex + 1;
                }
            }
            analyzeBeanList.add(getExportDataBean(method.getId(), method.getName(), "控制评估方法", controlIndex + i + 1));
        }

        List<RiskMeasureType> topTypeList = toolBean.getTopTypeList();
        if (CollectionUtils.isEmpty(topTypeList)) {
            throw new IllegalArgumentException("请至少配置一条管控措施类型");
        }
        //管控措施

        titles = excelTitleBean("管控措施类型", true, titles, measureTypeIndex + 1);
        analyzeBeanList.add(getExportDataBean(null, "管控措施类型", "管控措施类型", measureTypeIndex + 1));
        sheet.setDefaultColumnStyle(measureTypeIndex + 1, cellStyle);

        if (bean.getMaxTypeLevel() >= 2) {
            titles = excelTitleBean("管控措施二级类型", false, titles, measureTypeIndex + 2);
            analyzeBeanList.add(getExportDataBean(null, "管控措施二级类型", "管控措施二级类型", measureTypeIndex + 2));
            sheet.setDefaultColumnStyle(measureTypeIndex + 2, cellStyle);
        }
        if (bean.getMaxTypeLevel() >= 3) {
            titles = excelTitleBean("管控措施三级类型", false, titles, measureTypeIndex + 3);
            analyzeBeanList.add(getExportDataBean(null, "管控措施三级类型", "管控措施三级类型", measureTypeIndex + 3));
            sheet.setDefaultColumnStyle(measureTypeIndex + 3, cellStyle);
        }
        int measureTypeContentIndex = measureTypeIndex + 3 + 1;

        titles = excelTitleBean("管控措施内容", true, titles, measureTypeContentIndex);
        analyzeBeanList.add(getExportDataBean(null, "管控措施内容", "管控措施内容", measureTypeContentIndex));
        sheet.setDefaultColumnStyle(measureTypeContentIndex, cellStyle);

        dangerContentIndex = measureTypeContentIndex + 1;
        titles = excelTitleBean("隐患排查内容", false, titles, dangerContentIndex);
        analyzeBeanList.add(getExportDataBean(null, "隐患排查内容", "隐患排查内容", dangerContentIndex));
        sheet.setDefaultColumnStyle(dangerContentIndex, cellStyle);

        titles = excelTitleBean("排查岗位", false, titles, dangerContentIndex + 1);
        analyzeBeanList.add(getExportDataBean(null, "排查岗位", "排查岗位", dangerContentIndex + 1));

        titles = excelTitleBean("排查人员", false, titles, dangerContentIndex + 2);
        analyzeBeanList.add(getExportDataBean(null, "排查人员", "排查人员", dangerContentIndex + 2));

        titles = excelTitleBean("周期", false, titles, dangerContentIndex + 3);
        analyzeBeanList.add(getExportDataBean(null, "周期", "周期", dangerContentIndex + 3));

        titles = excelTitleBean("周期单位", false, titles, dangerContentIndex + 4);
        analyzeBeanList.add(getExportDataBean(null, "周期单位", "周期单位", dangerContentIndex + 4));
        RiskEvalExcelUtil.getSelectData(workbook, Lists.newArrayList("年", "月", "天", "时"), sheet, 1, 100, dangerContentIndex + 4, sheetIndex);
        sheetIndex = sheetIndex + 1;

        titles = excelTitleBean("隐患问题描述", false, titles, dangerContentIndex + 5);
        analyzeBeanList.add(getExportDataBean(null, "隐患问题描述", "隐患问题描述", dangerContentIndex + 5));

        titles = excelTitleBean("取数来源", false, titles, dangerContentIndex + 6);
        analyzeBeanList.add(getExportDataBean(null, "取数来源", "取数来源", dangerContentIndex + 6));
        if (!CollectionUtils.isEmpty(toolBean.getDataSourceList())) {
            List<String> nameList = toolBean.getDataSourceList().stream().map(BaseTypeGroupItem::getName).collect(Collectors.toList());
            RiskEvalExcelUtil.getSelectData(workbook, nameList, sheet, 1, 100, dangerContentIndex + 6, sheetIndex);
            sheetIndex = sheetIndex + 1;
        }

        titles = excelTitleBean("备注", false, titles, dangerContentIndex + 7);
        analyzeBeanList.add(getExportDataBean(null, "备注", "备注", dangerContentIndex + 7));

        titles = excelTitleBean("周期开始时间", false, titles, dangerContentIndex + 8);
        analyzeBeanList.add(getExportDataBean(null, "周期开始时间", "周期开始时间", dangerContentIndex + 8));

        titles = excelTitleBean("周期结束时间", false, titles, dangerContentIndex + 9);
        analyzeBeanList.add(getExportDataBean(null, "周期结束时间", "周期结束时间", dangerContentIndex + 9));

        evalTimeIndex = dangerContentIndex + 10;
        titles = excelTitleBean("评估日期", true, titles, evalTimeIndex);
        analyzeBeanList.add(getExportDataBean(null, "评估日期", "评估日期", evalTimeIndex));
        sheet.setDefaultColumnStyle(evalTimeIndex, cellStyle);

        titles = excelTitleBean("评估人", false, titles, evalTimeIndex + 1);
        analyzeBeanList.add(getExportDataBean(null, "评估人", "评估人", evalTimeIndex + 1));
        sheet.setDefaultColumnStyle(evalTimeIndex + 1, cellStyle);


        titles = excelTitleBean("责任单位", false, titles, evalTimeIndex + 2);
        analyzeBeanList.add(getExportDataBean(null, "责任单位", "责任单位", evalTimeIndex + 2));
        sheet.setDefaultColumnStyle(evalTimeIndex + 2, cellStyle);

        titles = excelTitleBean("管控级别", false, titles, evalTimeIndex + 3);
        analyzeBeanList.add(getExportDataBean(null, "管控级别", "管控级别", evalTimeIndex + 3));
        sheet.setDefaultColumnStyle(evalTimeIndex + 3, cellStyle);
        sheetIndex = fillControllLevelList(evalTimeIndex + 3, workbook, sheet, sheetIndex);
//        List<SimpleSelectListBean> controlLevelList = riskEvaluationBusinessService.getControlLevelList();
//        List<String> s1 = controlLevelList.stream().map(e -> e.getLabel()).collect(Collectors.toList());
//        if (!CollectionUtils.isEmpty(s1)) {
//            RiskEvalExcelUtil.getSelectData(workbook, s1, sheet, 1, 100, evalTimeIndex + 3, sheetIndex);
//            sheetIndex = sheetIndex + 1;
//        }
        titles = excelTitleBean("排查类型", false, titles, evalTimeIndex + 4);
        analyzeBeanList.add(getExportDataBean(null, "排查类型", "排查类型", evalTimeIndex + 4));
        if (!CollectionUtils.isEmpty(toolBean.getTroubleshootingTypeList())) {
            List<String> nameList = toolBean.getTroubleshootingTypeList().stream().map(BaseTypeGroupItem::getName).collect(Collectors.toList());
            RiskEvalExcelUtil.getSelectData(workbook, nameList, sheet, 1, 100, evalTimeIndex + 4, sheetIndex);
            sheetIndex = sheetIndex + 1;
        }


        //创建cell
        titles = titles.stream().sorted(Comparator.comparing(RiskExcelTitleBean::getIndex)).collect(Collectors.toList());
        for (int i = 0; i < titles.size(); i++) {
            cell = row.createCell(i);
            cell.setCellValue(titles.get(i).getTitle());
            if (titles.get(i).getIsRed()) {
                cell.setCellStyle(title(workbook, true));
            } else {
                cell.setCellStyle(title(workbook, false));
            }
        }

        //设置列宽 长度 = 风险点+风险点名称+分析方法+评估时间+评估方法+管控措施字段个数+分级管控字段个数
        for (int i = 0; i < titles.size(); i++) {
            sheet.setColumnWidth(i, 20 * 256);
        }
        //动态内容
        if (isTemplate != null && isTemplate.intValue() == 1) {
            workbook = getNewPointItemData(workbook, sheet, analyzeBeanList, toolBean, analyzeWayId, stablePointContrFlag, 1);
        }
        bean.setSheetIndex(sheetIndex);
        return workbook;
    }

    private SXSSFWorkbook getNewPointItemData(SXSSFWorkbook workbook, SXSSFSheet sheet, List<RiskExportDataBean> analyzeBeanList, ExcelExportToolMapBean toolBean, Long analyzeWayId, boolean stablePointContrFlag, Integer measureTypeFlag) {
        Map<Long, BaseUser> userMap = toolBean.getUserMap();
        Map<Long, BpDepart> departMap = toolBean.getDepartMap();
        Map<Long, String> dataSourceMap = toolBean.getDataSourceMap();
        Map<Long, String> troubleshootingTypeMap = toolBean.getTroubleshootingTypeMap();
        Map<Long, RiskControllevels> controLevelMap = toolBean.getControLevelMap();
        Map<Long, RiskEvaluationTargetItem> targetItemMap = toolBean.getTargetItemMap();
        Map<String, RiskPointType> pointTypeMap = toolBean.getPointTypeMap();
        Map<Long, List<RiskPointEvaluationItem>> evaluationItemMap = toolBean.getEvaluationItemMap();
        List<RiskPoint> points = toolBean.getPoints();
        Map<Long, String> jobMap = toolBean.getJobMap();
        Map<Long, List<RiskHiddenDangerContent>> dangerContentMap = toolBean.getDangerContentMap();
        Map<Long, List<BpInvestigatePlan>> investigatePlanMap = toolBean.getInvestigatePlanMap();
        if (!CollectionUtils.isEmpty(points)) {
            //行号
            int index = 1;
            for (RiskPoint point : points) {
                Map<Long, String> staContents = new LinkedHashMap<>();
                RiskPointEvaluationBusiness evaluation = pointEvaluationBusinessDao.getLastPointEvaluation(point.getId(), 1);
                if (evaluation != null && !evaluation.getAnalyzeWayId().equals(analyzeWayId)) {
                    continue;
                }
                if (evaluation != null && stablePointContrFlag && evaluation.getStableEvaluationId() != null) {
                    List<RiskPointEvaluationDetail> stableEvalIdList = riskPointEvaluationDetailDao.findByPointEvaluationIdAndMethodType(evaluation.getStableEvaluationId(), 1);
                    if (!CollectionUtils.isEmpty(stableEvalIdList)) {
                        for (RiskPointEvaluationDetail detail : stableEvalIdList) {
                            if (detail.getContentType() != null && detail.getContentType().equals(0)) {
                                if (detail.getTargetItemId() != null) {
                                    if (!CollectionUtils.isEmpty(targetItemMap)) {
                                        RiskEvaluationTargetItem targetItem = targetItemMap.get(detail.getTargetItemId());
                                        if (targetItem != null && targetItem.getScore() != null) {
                                            staContents.put(detail.getMethodId(), targetItem.getScore().toString());
                                        }
                                    }
                                }
                            } else {
                                //积
                                if (StringUtils.isNotBlank(detail.getContent())) {
                                    staContents.put(detail.getMethodId(), detail.getContent());
                                }
                            }
                        }
                    }
                }
                //风险点编号
                String pointCode = point.getCode();
                //风险点名称
                String pointName = point.getName();
                //风险点类型
                String pTypeName = null;
                if (!CollectionUtils.isEmpty(pointTypeMap)) {
                    RiskPointType pType = pointTypeMap.get(point.getType());
                    if (pType == null) {
                        //兼容部分将类型名称当做编号的旧数据
                        pType = riskPointTypeDao.findFirstByName(point.getType());
                        if (pType == null) {
                            throw new IllegalArgumentException("风险分析+(" + point.getCode() + ")类型配置不正确");
                        }
                    }
                    pTypeName = pType.getName();
                }
                String resEvalDepartName = null;
                String resEvalUserName = null;
                //风险区域名称
                String riskDistrictName = null;
                if (point.getRiskDistrict() != null) {
                    riskDistrictName = point.getRiskDistrict().getName();
                    if (point.getRiskDistrict().getResponsibleDept() != null && departMap.get(point.getRiskDistrict().getResponsibleDept()) != null) {
                        resEvalDepartName = departMap.get(point.getRiskDistrict().getResponsibleDept()).getName();
                    }
                    if (StringUtils.isNotBlank(point.getRiskDistrict().getResponsibleUser())) {
                        List<Long> userIds = JSONArray.parseArray(point.getRiskDistrict().getResponsibleUser(), Long.class);
                        resEvalUserName = getUserNameOrCodeStr(userIds, userMap, "name");
                    }
                }
                //评估人
                String evaluationUserNames = null;
                //评估人工号
                String evaluationUserCodes = null;
                Map<Long, List<RiskPointEvaluationDetail>> itemDeatilMap = new HashMap<>();
                if (evaluation != null) {
                    List<RiskPointEvaluationDetail> detailList = riskPointEvaluationDetailDao.findByPointEvaluationId(evaluation.getId());
                    if (!CollectionUtils.isEmpty(detailList)) {
                        itemDeatilMap = detailList.stream().collect(Collectors.groupingBy(RiskPointEvaluationDetail::getItemId));
                    }
                    if (StringUtils.isNotBlank(evaluation.getEvaluationUserIds())) {
                        List<Long> evaluationUserIds = StringUtil.covertJsonToLongList(evaluation.getEvaluationUserIds());
                        evaluationUserNames = getUserNameOrCodeStr(evaluationUserIds, userMap, "name");
                        evaluationUserCodes = getUserNameOrCodeStr(evaluationUserIds, userMap, "code");
                    }
                    //评估日期
                    String evaluationDate = SimpleDateFormatCache.getYmd().format(evaluation.getEvaluationDate());
                    if (!CollectionUtils.isEmpty(evaluationItemMap)) {
                        //----item内容
                        List<RiskPointEvaluationItem> itemList = evaluationItemMap.get(evaluation.getId());
                        if (!CollectionUtils.isEmpty(itemList)) {
                            for (int i = 0; i < itemList.size(); i++) {
                                RiskPointEvaluationjsonItemBean itemBean = riskEvaluationBusinessService.getjsonItemBean(itemList, targetItemMap, itemDeatilMap, i, toolBean, "detail-plan");
                                if (itemBean != null && !CollectionUtils.isEmpty(itemBean.getContentList())) {
                                    // 管控层级
                                    List<Long> controlLevelIds = itemBean.getControlLevelIds();
                                    String controLevelNames = getControLevelNames(controlLevelIds, controLevelMap);
                                    //责任单位
                                    List<Long> responseDepartIds = itemBean.getResponseDepartIds();
                                    String responseDepartNames = getDepartNameOrCodeStr(responseDepartIds, departMap, "name");
                                    String responseDepartCodes = getDepartNameOrCodeStr(responseDepartIds, departMap, "code");
                                    List<RiskEvaluationContentDetailBean> contentList = itemBean.getContentList();
                                    for (RiskEvaluationContentDetailBean contentDetailBean : contentList) {
                                        if (!CollectionUtils.isEmpty(dangerContentMap) && contentDetailBean.getDetailId() != null) {
                                            List<RiskHiddenDangerContent> riskHiddenDangerContents = dangerContentMap.get(contentDetailBean.getDetailId());
                                            if (!CollectionUtils.isEmpty(riskHiddenDangerContents)) {
                                                for (RiskHiddenDangerContent dContent : riskHiddenDangerContents) {
                                                    if (!CollectionUtils.isEmpty(investigatePlanMap) && !CollectionUtils.isEmpty(investigatePlanMap.get(dContent.getId()))) {
                                                        List<BpInvestigatePlan> plans = investigatePlanMap.get(dContent.getId());
                                                        if (!CollectionUtils.isEmpty(plans)) {
                                                            for (BpInvestigatePlan plan : plans) {
                                                                SXSSFRow row = sheet.createRow(index);
                                                                // baseCellDataInput(row, riskDistrictName, pointCode, pointName, pTypeName, i);
                                                                //排查人员
                                                                String executor = plan.getExecutor();
                                                                String executorNames = "";
                                                                if (StringUtils.isNotBlank(executor)) {
                                                                    executorNames = getUserNameOrCodeStr(JSONArray.parseArray(executor, Long.class), userMap, "name");
                                                                }
                                                                //周期单位
                                                                String periodUnit = plan.getPeriodUnit();
                                                                //周期
                                                                Integer periodDays = plan.getPeriodDays();
                                                                String periodJson = plan.getPeriodJson();
                                                                String checkRateDescribe = plan.getCheckRateDescribe();
                                                                String cycleFrequency = getCycleFrequency(periodUnit, periodJson, periodDays,checkRateDescribe);
                                                                covertContentDetail(analyzeBeanList, row, itemBean, staContents, measureTypeFlag, null, contentDetailBean, riskDistrictName, pointCode, pointName, pTypeName, i,
                                                                        evaluationUserNames, evaluationUserCodes, evaluationDate, userMap, responseDepartNames, responseDepartCodes,
                                                                        controLevelNames, dContent, plan, jobMap, executorNames, cycleFrequency, dataSourceMap, troubleshootingTypeMap, resEvalDepartName, resEvalUserName);
                                                                index++;
                                                            }
                                                        }
                                                    } else if (CollectionUtils.isEmpty(investigatePlanMap.get(dContent.getId()))) {
                                                        SXSSFRow row = sheet.createRow(index);
                                                        //baseCellDataInput(row, riskDistrictName, pointCode, pointName, pTypeName, i);
                                                        covertContentDetail(analyzeBeanList, row, itemBean, staContents, measureTypeFlag, null, contentDetailBean, riskDistrictName, pointCode, pointName, pTypeName, i,
                                                                evaluationUserNames, evaluationUserCodes, evaluationDate, userMap, responseDepartNames, responseDepartCodes,
                                                                controLevelNames, dContent, null, jobMap, "", "", dataSourceMap, troubleshootingTypeMap, resEvalDepartName, resEvalUserName);
                                                        index++;
                                                    }
                                                }
                                            } else {
                                                //排查内容为空 管控措施不为空的数据也要导出来
                                                SXSSFRow row = sheet.createRow(index);
                                                // baseCellDataInput(row, riskDistrictName, pointCode, pointName, pTypeName, i);
                                                covertContentDetail(analyzeBeanList, row, itemBean, staContents, measureTypeFlag, null, contentDetailBean, riskDistrictName, pointCode, pointName, pTypeName, i,
                                                        evaluationUserNames, evaluationUserCodes, evaluationDate, userMap, responseDepartNames, responseDepartCodes,
                                                        controLevelNames, null, null, jobMap, "", "", dataSourceMap, troubleshootingTypeMap, resEvalDepartName, resEvalUserName);
                                                index++;

                                            }
                                        } else if (CollectionUtils.isEmpty(dangerContentMap) && contentDetailBean.getDetailId() != null) {
                                            //排查内容为空 管控措施不为空的数据也要导出来
                                            SXSSFRow row = sheet.createRow(index);
                                            // baseCellDataInput(row, riskDistrictName, pointCode, pointName, pTypeName, i);
                                            covertContentDetail(analyzeBeanList, row, itemBean, staContents, measureTypeFlag, null, contentDetailBean, riskDistrictName, pointCode, pointName, pTypeName, i,
                                                    evaluationUserNames, evaluationUserCodes, evaluationDate, userMap, responseDepartNames, responseDepartCodes,
                                                    controLevelNames, null, null, jobMap, "", "", dataSourceMap, troubleshootingTypeMap, resEvalDepartName, resEvalUserName);
                                            index++;
                                        }

                                    }
                                }
                            }
                        }
                    }
                }

            }
        }
        return workbook;
    }

    private SXSSFWorkbook getShanDongNewPointItemData(SXSSFWorkbook workbook, SXSSFSheet sheet, List<RiskExportDataBean> analyzeBeanList, ExcelExportToolMapBean toolBean, Long analyzeWayId, boolean stablePointContrFlag) {
        Map<Long, BaseUser> userMap = toolBean.getUserMap();
        Map<Long, BpDepart> departMap = toolBean.getDepartMap();
        Map<Long, String> dataSourceMap = toolBean.getDataSourceMap();
        Map<Long, RiskControllevels> controLevelMap = toolBean.getControLevelMap();
        Map<Long, RiskEvaluationTargetItem> targetItemMap = toolBean.getTargetItemMap();
        Map<String, RiskPointType> pointTypeMap = toolBean.getPointTypeMap();
        Map<Long, List<RiskPointEvaluationItem>> evaluationItemMap = toolBean.getEvaluationItemMap();
        List<RiskPoint> points = toolBean.getPoints();
        Map<Long, String> jobMap = toolBean.getJobMap();
//        Map<Long, List<RiskHiddenDangerContent>> dangerContentMap = toolBean.getDangerContentMap();
//        Map<Long, List<BpInvestigatePlan>> investigatePlanMap = toolBean.getInvestigatePlanMap();
        if (!CollectionUtils.isEmpty(points)) {
            //行号
            int index = 1;
            for (RiskPoint point : points) {
                Map<Long, String> staContents = new LinkedHashMap<>();
                RiskPointEvaluationBusiness evaluation = pointEvaluationBusinessDao.getLastPointEvaluation(point.getId(), 1);
                if (evaluation != null && !evaluation.getAnalyzeWayId().equals(analyzeWayId)) {
                    continue;
                }
                if (evaluation != null && stablePointContrFlag && evaluation.getStableEvaluationId() != null) {
                    List<RiskPointEvaluationDetail> stableEvalIdList = riskPointEvaluationDetailDao.findByPointEvaluationIdAndMethodType(evaluation.getStableEvaluationId(), 1);
                    if (!CollectionUtils.isEmpty(stableEvalIdList)) {
                        for (RiskPointEvaluationDetail detail : stableEvalIdList) {
                            if (detail.getContentType() != null && detail.getContentType().equals(0)) {
                                if (detail.getTargetItemId() != null) {
                                    if (!CollectionUtils.isEmpty(targetItemMap)) {
                                        RiskEvaluationTargetItem targetItem = targetItemMap.get(detail.getTargetItemId());
                                        if (targetItem != null && targetItem.getScore() != null) {
                                            staContents.put(detail.getMethodId(), targetItem.getScore().toString());
                                        }
                                    }
                                }
                            } else {
                                //积
                                if (StringUtils.isNotBlank(detail.getContent())) {
                                    staContents.put(detail.getMethodId(), detail.getContent());
                                }
                            }
                        }
                    }
                }
                //风险点编号
                String pointCode = point.getCode();
                //风险点名称
                String pointName = point.getName();
                //风险点类型
                String pTypeName = null;
                if (!CollectionUtils.isEmpty(pointTypeMap)) {
                    RiskPointType pType = pointTypeMap.get(point.getType());
                    if (pType == null) {
                        //兼容部分将类型名称当做编号的旧数据
                        pType = riskPointTypeDao.findFirstByName(point.getType());
                        if (pType == null) {
                            throw new IllegalArgumentException("风险单元+(" + point.getCode() + ")类型配置不正确");
                        }
                    }
                    pTypeName = pType.getName();
                }
                //风险区域名称
                String riskDistrictName = null;
                if (point.getRiskDistrict() != null) {
                    riskDistrictName = point.getRiskDistrict().getName();
                }
                //评估人
                String evaluationUserNames = null;
                //评估人工号
                String evaluationUserCodes = null;
                Map<Long, List<RiskPointEvaluationDetail>> itemDeatilMap = new HashMap<>();
                if (evaluation != null) {
                    List<RiskPointEvaluationDetail> detailList = riskPointEvaluationDetailDao.findByPointEvaluationId(evaluation.getId());
                    if (!CollectionUtils.isEmpty(detailList)) {
                        itemDeatilMap = detailList.stream().collect(Collectors.groupingBy(RiskPointEvaluationDetail::getItemId));
                    }
                    if (StringUtils.isNotBlank(evaluation.getEvaluationUserIds())) {
                        List<Long> evaluationUserIds = StringUtil.covertJsonToLongList(evaluation.getEvaluationUserIds());
                        evaluationUserNames = getUserNameOrCodeStr(evaluationUserIds, userMap, "name");
                        evaluationUserCodes = getUserNameOrCodeStr(evaluationUserIds, userMap, "code");
                    }
                    //评估日期
                    String evaluationDate = SimpleDateFormatCache.getYmd().format(evaluation.getEvaluationDate());
                    if (!CollectionUtils.isEmpty(evaluationItemMap)) {
                        //----item内容
                        List<RiskPointEvaluationItem> itemList = evaluationItemMap.get(evaluation.getId());
                        if (!CollectionUtils.isEmpty(itemList)) {
                            for (int i = 0; i < itemList.size(); i++) {
                                RiskPointEvaluationjsonItemBean itemBean = riskEvaluationBusinessService.getjsonItemBean(itemList, targetItemMap, itemDeatilMap, i, toolBean, "detail-plan");
                                if (itemBean != null && !CollectionUtils.isEmpty(itemBean.getContentList())) {
                                    // 管控层级
                                    List<Long> controlLevelIds = itemBean.getControlLevelIds();
                                    String controLevelNames = getControLevelNames(controlLevelIds, controLevelMap);
                                    //责任单位
                                    List<Long> responseDepartIds = itemBean.getResponseDepartIds();
                                    String responseDepartNames = getDepartNameOrCodeStr(responseDepartIds, departMap, "name");
                                    String responseDepartCodes = getDepartNameOrCodeStr(responseDepartIds, departMap, "code");
                                    List<RiskEvaluationContentDetailBean> contentList = itemBean.getContentList();

                                    //排查内容为空 管控措施不为空的数据也要导出来
                                    SXSSFRow row = sheet.createRow(index);
                                    covertContentDetail(analyzeBeanList, row, itemBean, staContents, 0, contentList, null, riskDistrictName, pointCode, pointName, pTypeName, i,
                                            evaluationUserNames, evaluationUserCodes, evaluationDate, userMap, responseDepartNames, responseDepartCodes,
                                            controLevelNames, null, null, jobMap, "", "", dataSourceMap, null, null, null);
                                    index++;

                                }
                            }
                        }
                    }
                }

            }
        }
        return workbook;
    }

    //设置抬头是否必填
    private List<RiskExcelTitleBean> excelTitleBean(String title, Boolean isRed, List<RiskExcelTitleBean> titles, Integer index) {
        RiskExcelTitleBean bean = new RiskExcelTitleBean();
        bean.setTitle(title);
        bean.setIsRed(isRed);
        bean.setIndex(index);
        titles.add(bean);
        return titles;
    }


    //获取列数据
    private RiskExportDataBean getExportDataBean(Long id, String name, String type, Integer index) {
        RiskExportDataBean bean = new RiskExportDataBean();
        bean.setId(id);
        bean.setName(name);
        bean.setType(type);
        bean.setIndex(index);
        return bean;
    }

    // 填充管控措施类型下拉列表
    private int fillMeasureTypeList(Integer typeLevel, int index, SXSSFWorkbook workbook, SXSSFSheet sheet, int sheetIndex) {
        List<String> list = riskMeasureTypeService.getNamesByTypeLevel(typeLevel);
        if (!CollectionUtils.isEmpty(list)) {
            RiskEvalExcelUtil.getSelectData(workbook, list, sheet, 1, 100, index, sheetIndex);
            sheetIndex++;
        }
        return sheetIndex;
    }

    // 填充管控层级下拉列表
    private int fillControllLevelList(int index, SXSSFWorkbook workbook, SXSSFSheet sheet, int sheetIndex) {
        List<SimpleSelectListBean> controllLevels = riskControllevelService.getAllForSimpleSelect();
        List<String> list = controllLevels.stream().map(SimpleSelectListBean::getLabel).collect(toList());
        if (!CollectionUtils.isEmpty(list)) {
            RiskEvalExcelUtil.getSelectData(workbook, list, sheet, 1, 100, index, sheetIndex);
            sheetIndex++;
        }
        return sheetIndex;
    }

    //小标题的样式
    public CellStyle title(Workbook wb, Boolean isRed) {
        return RiskEvalExcelUtil.title(wb, isRed, 12);
    }

    //文字样式
    public CellStyle text(Workbook wb) {
        return RiskEvalExcelUtil.text(wb, 10, false);
    }

    private String getUserNameOrCodeStr(List<Long> userIds, Map<Long, BaseUser> userMap, String flag) {
        String str = "";
        if (!CollectionUtils.isEmpty(userIds) && !CollectionUtils.isEmpty(userMap)) {
            for (Long id : userIds) {
                BaseUser user = userMap.get(id);
                if (user != null) {
                    if (StringUtils.isNotBlank(flag) && flag.equals("name")) {
                        str = str + user.getRealName() + ",";
                    } else {
                        str = str + user.getCode() + ",";
                    }
                }
            }
            if (StringUtils.isNotBlank(str)) {
                str = str.substring(0, str.length() - 1);
            }
        }
        return str;
    }

    private String getDepartNameOrCodeStr(List<Long> departIds, Map<Long, BpDepart> departMap, String flag) {
        String str = "";
        if (!CollectionUtils.isEmpty(departIds) && !CollectionUtils.isEmpty(departMap)) {
            for (Long id : departIds) {
                BpDepart depart = departMap.get(id);
                if (depart != null) {
                    if (StringUtils.isNotBlank(flag) && flag.equals("name")) {
                        str = str + depart.getName() + ",";
                    } else {
                        str = str + depart.getCode() + ",";
                    }
                }
            }
            if (StringUtils.isNotBlank(str)) {
                str = str.substring(0, str.length() - 1);
            }
        }
        return str;
    }

    private String getControLevelNames(List<Long> levelIds, Map<Long, RiskControllevels> controLevelMap) {
        String str = "";
        if (!CollectionUtils.isEmpty(levelIds) && !CollectionUtils.isEmpty(controLevelMap)) {
            for (Long id : levelIds) {
                RiskControllevels cl = controLevelMap.get(id);
                if (cl != null) {
                    str = str + cl.getName() + ",";
                }
            }
            if (StringUtils.isNotBlank(str)) {
                str = str.substring(0, str.length() - 1);
            }
        }
        return str;
    }


    private void covertContentDetail(List<RiskExportDataBean> analyzeBeanList, SXSSFRow row, RiskPointEvaluationjsonItemBean itemBean, Map<Long, String> staContents,
                                     Integer measureTypeFlag, List<RiskEvaluationContentDetailBean> contentList, RiskEvaluationContentDetailBean contentDetailBean, String riskDistrictName, String pointCode, String pointName, String pTypeName, int i, String evaluationUserNames,
                                     String evaluationUserCodes, String evaluationDate, Map<Long, BaseUser> userMap, String responseDepartNames, String responseDepartCodes,
                                     String controLevelNames, RiskHiddenDangerContent dContent, BpInvestigatePlan plan, Map<Long, String> jobMap,
                                     String executorNames, String cycleFrequency, Map<Long, String> dataSourceMap, Map<Long, String> troubleshootingTypeMap, String resEvalDepartName, String resEvalUserName) {
        for (RiskExportDataBean rb : analyzeBeanList) {
            SXSSFCell cell = row.createCell(rb.getIndex());
            cell.setCellType(CellType.STRING);
            BaseUser bu;
            switch (rb.getType()) {
                case "风险分析对象名称":
                    cell.setCellValue(riskDistrictName);
                    break;
                case "风险分析对象":
                    cell.setCellValue(riskDistrictName);
                    break;
                case "区域责任部门":
                    cell.setCellValue(resEvalDepartName);
                    break;
                case "区域责任人":
                    cell.setCellValue(resEvalUserName);
                    break;
                case "风险单元编号":
                    cell.setCellValue(pointCode);
                    break;
                case "风险单元名称":
                    cell.setCellValue(pointName);
                    break;
                case "风险分析单元":
                    cell.setCellValue(pointName);
                    break;
                case "风险单元类型":
                    cell.setCellValue(pTypeName);
                    break;
                case "序号":
                    cell.setCellValue(i + 1);
                    break;
                case "分析方法":
                    cell.setCellValue(itemBean.getAnalyseContent().get(rb.getId()));
                    break;
                case "控制评估方法":
                    cell.setCellValue(itemBean.getStableContents().get(rb.getId()));
                    break;
                case "固有评估方法":
                    if (!CollectionUtils.isEmpty(staContents)) {
                        cell.setCellValue(staContents.get(rb.getId()));
                    }
                    break;
                case "管控措施类型":
                    if (measureTypeFlag == 1) {
                        if (contentDetailBean != null) {
                            String measure1 = contentDetailBean.getMeasure1();
                            if (measureTypeFlag == 1) {
                                cell.setCellValue(measure1);
                            }
                        }
                    } else if (measureTypeFlag == 0) {
                        for (RiskEvaluationContentDetailBean bean : contentList) {
                            if (measureTypeFlag == 0 && rb.getName().equals(bean.getMeasure1())) {
                                cell.setCellValue(bean.getContent());
                            }
                        }
                    }
                    break;
                case "管控措施二级类型":
                    if (contentDetailBean != null) {
                        cell.setCellValue(contentDetailBean.getMeasure2());
                    }
                    break;
                case "管控措施三级类型":
                    if (contentDetailBean != null) {
                        cell.setCellValue(contentDetailBean.getMeasure3());
                    }
                    break;
                case "管控措施内容":
                    if (measureTypeFlag == 1 && contentDetailBean != null) {
                        cell.setCellValue(contentDetailBean.getContent());
                    }
                    break;
                case "隐患排查内容":
                    if (dContent != null) {
                        cell.setCellValue(dContent.getContent());
                    }
                    break;
                case "评估人工号":
                    cell.setCellValue(evaluationUserCodes);
                    break;
                case "评估人":
                    cell.setCellValue(evaluationUserNames);
                    break;
                case "评估日期":
                    cell.setCellValue(evaluationDate);
                    break;
                case "责任人工号":
                    bu = userMap.get(itemBean.getResponseUserIds().get(0));
                    if (bu != null) {
                        cell.setCellValue(bu.getCode());
                    }
                    break;
                case "责任人":
                    bu = userMap.get(itemBean.getResponseUserIds().get(0));
                    if (bu != null) {
                        cell.setCellValue(bu.getRealName());
                    }
                    break;
                case "责任单位编码":
                    cell.setCellValue(responseDepartCodes);
                    break;
                case "责任单位":
                    cell.setCellValue(responseDepartNames);
                    break;
                case "管控级别":
                    cell.setCellValue(controLevelNames);
                    break;
                case "隐患问题描述":
                    if (plan != null) {
                        cell.setCellValue(plan.getHiddenDangersDesc());
                    }
                    break;
                case "排查岗位":
                    if (plan != null) {
                        Integer executiveUnit = plan.getExecutiveUnit();
                        if (executiveUnit != null && executiveUnit == 1) {
                            cell.setCellValue(jobMap.get(plan.getUnitId()));
                        } else if (executiveUnit != null && executiveUnit == 0) {
                            cell.setCellValue(jobMap.get(plan.getCheckJob()));
                        }
                    }
                    break;
                case "排查人员":
                    cell.setCellValue(executorNames);
                    break;
                case "周期":
                    cell.setCellValue(cycleFrequency);
                    break;
                case "周期单位":
                    if (plan != null) {
                        String periodUnit = plan.getPeriodUnit();
                        if ("周".equals(plan.getPeriodUnit()) || "日".equals(plan.getPeriodUnit())){
                            periodUnit = "天";
                        }
                        cell.setCellValue(periodUnit);
                    }
                    break;
                case "周期开始时间":
                    if (plan != null && "时".equals(plan.getPeriodUnit())) {
                        String hour = "0点";
                        if (plan.getPlanStartTime() != null){
                            hour = DateUtil.hour(plan.getPlanStartTime(),true)+"点";
                        }
                        cell.setCellValue(hour);
                    }
                    break;
                case "周期结束时间":
                    if (plan != null && "时".equals(plan.getPeriodUnit())) {
                        String hour = "24点";
                        if (plan.getPlanEndTime() != null){
                            hour = DateUtil.hour(plan.getPlanEndTime(),true)+"点";
                        }
                        cell.setCellValue(hour);
                    }
                    break;
                case "取数来源":
                    if (dContent != null) {
                        cell.setCellValue(dataSourceMap.get(dContent.getDateSource()));
                    }
                    break;
                case "备注":
                    if (plan != null) {
                        cell.setCellValue(plan.getRemark());
                    }
                    break;
                case "排查类型":
                    if (plan != null) {
                        cell.setCellValue(troubleshootingTypeMap.get(plan.getTroubleshootingType()));
                    }
                    break;
            }
        }
    }

    private String getCycleFrequency(String periodUnit, String periodJson, Integer periodDays,String checkRateDescribe) {
        String cycleFrequency = "";
        if (periodUnit.equals("周")) {
//            if (StringUtils.isNotBlank(periodJson)) {
//                List<Integer> list = JSONArray.parseArray(periodJson, Integer.class);
//                for (Integer p : list) {
//                    cycleFrequency = cycleFrequency + p + ",";
//                }
//                if (StringUtils.isNotBlank(cycleFrequency)) {
//                    cycleFrequency = cycleFrequency.substring(0, cycleFrequency.length() - 1);
//                }
//            }
            if (StringUtils.isNotBlank(checkRateDescribe)){
                cycleFrequency = checkRateDescribe.replace("每","").replace("、",",");
            }
        } else {
            cycleFrequency = String.valueOf(periodDays);
        }
        return cycleFrequency;
    }

}
